﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    public static string orderId = null;
    public static string wId = null;
    public static int startnum = 0;
    public static DateTime preturntime = new DateTime();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            startnum = 0;
            try
            {
                orderId = Request.QueryString["orderId"].ToString();
                //orderId = "OR201011280000";
                lbwelcome.Text = "操作订单号为<font color='red'>" + orderId + "</font>下面请选择需要服务内容...";
            }
            catch (Exception ee)
            {

                Response.Write("<script>alert('请填写订单！');history.back();</script>");
                //Server.Transfer("GetOrderInfo.aspx");
                Response.Redirect("GetOrderInfo.aspx");
            }
            SOAWebService.Service1 ser = new SOAWebService.Service1();
            DataTable dt = ser.getType();
            CheckBoxList1.DataSource = dt;

            CheckBoxList1.DataTextField = "_typename";
            CheckBoxList1.DataValueField = "_typeId";
            CheckBoxList1.DataBind();
            Imgcheck.Visible = false;
            lbcheck.Visible = false;

            imgMcheck.Visible = false;
            lbMcheck.Visible = false;
            //
            this.getWorkplaceInfo();
            for (int i = 0; i < wizard1.WizardSteps.Count; i++)
                wizard1.WizardSteps[i].AllowReturn = false;
        }


    }
    /// <summary>
    /// 显示所填零件
    /// </summary>
    public void validtorPartsNum()
    {
        List<Parts> partslist = new List<Parts>();


        string[] partsstr = hidden1.Value.Split(';');
        for (int i = 0; i < partsstr.Length - 1; i++)
        {
            Parts partstemp = new Parts();
            string[] partsone = partsstr[i].Split(',');
            partstemp.PartsId = partsone[0];
            partstemp.PartsNum = int.Parse(partsone[1].ToString());
            partslist.Add(partstemp);
        }
        if (partsstr.Length == 1)
        {
            Response.Write("<script>alert('获取内容为空！');history.back();</script>");
        }
        GridView1.DataSource = partslist;
        GridView1.DataBind();

    }
    public void showmachine()
    {
        List<Machine> machinelist = new List<Machine>();
        string[] macId = Machidden.Value.Split(',');
        for (int i = 0; i < macId.Length - 1; i++)
        {
            Machine mac = new Machine();
            mac.MachineId = macId[i];
            machinelist.Add(mac);
        }
        if (macId.Length == 1)
        {
            Response.Write("<script>alert('获取内容为空！');history.back();</script>");
        }
        GridView2.DataSource = machinelist;
        GridView2.DataBind();
    }
    /// <summary>
    /// 验证机器库存
    /// </summary>
    public void validatorMachineNum()
    {


        //    string[] partsstr = hidden1.Value.Split(';');
        //    for (int i = 0; i < partsstr.Length - 1; i++)
        //    {
        //        Parts partstemp = new Parts();
        //        string[] partsone = partsstr[i].Split(',');
        //        partstemp.PartsId = partsone[0];
        //        partstemp.PartsNum = int.Parse(partsone[1].ToString());
        //        partslist.Add(partstemp);
        //    }

        //    GridView1.DataSource = partslist;
        //    GridView1.DataBind();
    }
    /// <summary>
    ///将服务记录插入数据库
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        //插入类型
        bool typeInsertSuccess;
        StringBuilder mystring = new StringBuilder();
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected)
            {
                SOAWebService.Service1 ser = new SOAWebService.Service1();
                typeInsertSuccess = ser.ordertypeInsert(orderId, int.Parse(CheckBoxList1.Items[i].Value));
                //mystring.Append(CheckBoxList1.Items[i].Value);
            }
        }
        //插入工时
        using (SOAWebService.Service1 ser = new SOAWebService.Service1())
        {
            bool updateUtime = ser.updateworkplan(wId, float.Parse(txtUseTime.Text.Trim()));
            if (updateUtime)
                txtUseTime.ReadOnly = true;
        }
        //插入。。
        insertWorkplanParts();
        //插入。。
        insertWorkplanMachine();
        //更新订单开始时间
        updateOrderDate();
        Response.Redirect("workPlanView.aspx?oId="+orderId);

    }
    /// <summary>
    /// 更新订单开始时间
    /// </summary>
    public void updateOrderDate()
    {
        using (SOAWebService.Service1 ser = new SOAWebService.Service1())
        {
            ser.updateOrderStartdate(preturntime, orderId);
        }
    }
    /// <summary>
    /// 验证服务类型是否选择
    /// </summary>
    public void checkboxValidator()
    {
        int checkCount = 0;
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected)
            {
                checkCount++;
            }
        }

        if (checkCount > 0)
        {
            TextBox1.Text = checkCount.ToString();

        }
        else
        {
            TextBox1.Text = "";
        }

    }
    /// <summary>
    /// 验证是否选择工作场地,并向workplan中插入数据
    /// </summary>
    public void checkworkplacechoice()
    {
        int count = 0;

        if (wizard1.ActiveStepIndex == 2)
        {

            for (int i = 0; i < GridView3.Rows.Count; i++)
            {
                if (((CheckBox)GridView3.Rows[i].FindControl("checkplace")).Checked)
                {
                    count++;
                }
            }
            if (count == 1)
            {
                txtworkplaceyanzheng.Text = count.ToString();
                startnum++;
                if (startnum == 2)
                {
                    try
                    {
                        SOAWebService.Service1 ser = new SOAWebService.Service1();
                        //获取workplanID值，并输入最新值
                        string rpYear = string.Format("{0:D4}", int.Parse(DateTime.Now.Year.ToString()));
                        string rpMonth = string.Format("{0:D2}", int.Parse(DateTime.Now.Month.ToString()));
                        string rpDay = string.Format("{0:D2}", int.Parse(DateTime.Now.Day.ToString()));
                        string rpDate = rpYear + rpMonth + rpDay;
                        DataTable dt = ser.getWorkPlanId();
                        int precode = 0;
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            string predate = dt.Rows[i]["_planId"].ToString().Substring(2, 8);
                            string prenum = dt.Rows[i]["_planId"].ToString().Substring(10, 4);
                            if (predate.Trim() == rpDate.Trim())
                            {
                                if (int.Parse(prenum) > precode)
                                {
                                    precode = int.Parse(prenum);
                                }
                            }
                        }
                        string nowcode = string.Format("{0:D4}", precode + 1);
                        wId = "Pl" + rpDate + nowcode;

                        string placeId = null;
                        for (int i = 0; i < GridView3.Rows.Count; i++)
                        {
                            if (((CheckBox)GridView3.Rows[i].FindControl("checkplace")).Checked)
                            {
                                placeId = GridView3.Rows[i].Cells[1].Text;
                                //获取厂房的最晚返回时间并放入preturntime
                                string wdate = ((Label)GridView3.Rows[i].FindControl("lbrtime")).Text;
                                if (preturntime <Convert.ToDateTime(wdate))
                                {
                                    preturntime = Convert.ToDateTime(wdate);
                                }


                            }
                        }
                        bool wpsuccess = ser.workplanInsert(wId, orderId, placeId, 0);
                        startnum = 0;


                    }
                    catch (Exception e)
                    {
                        Response.Write(e.Message);
                    }
                }
            }
            else
            {
                txtworkplaceyanzheng.Text = "";
            }
        }



    }

    /// <summary>
    /// 下一步时验证服务类型是否选择
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
    {
        this.checkboxValidator();
        this.checkworkplacechoice();
        if (wizard1.ActiveStepIndex == 3)
        {
            this.validtorPartsNum();
        }
        if (wizard1.ActiveStepIndex == 5)
        {
            this.showmachine();
        }
        //根据所需内容控制需要填写的页面
        using (SOAWebService.Service1 ser = new SOAWebService.Service1())
        {
            SOAWebService.Order ord = ser.getOrderById(orderId);
            bool isuseparts = (bool)ord._isuseParts;
            //if (isuseparts == false && ord._isuseMachine == false)
            //{
            //    //wizard1.WizardSteps[2].Visible = false;
            //    if (wizard1.ActiveStepIndex == 1)
            //    {

            //        wizard1.ActiveStepIndex = 6;
            //        //wizard1.WizardSteps[2].AllowReturn = false;
            //        //wizard1.WizardSteps[3].AllowReturn = false;
            //        //wizard1.WizardSteps[4].AllowReturn = false;
            //        //wizard1.WizardSteps[5].AllowReturn = false;
            //        //wizard1.WizardSteps[6].AllowReturn = false;
            //    }
            //}
            //if (isuseparts == false)
            //{
            //    if (wizard1.ActiveStepIndex == 1)
            //    {
            //        wizard1.ActiveStepIndex = 4;
            //        wizard1.WizardSteps[4].AllowReturn = false;
            //    }
            //}
            //if (ord._isuseMachine == false)
            //{
            //    if (wizard1.ActiveStepIndex == 3)
            //    {
            //        wizard1.ActiveStepIndex = 6;
            //        wizard1.WizardSteps[6].AllowReturn = false;
            //    }
            //}
        }



    }

    /// <summary>
    /// 载入页面验证服务类型是否选择
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void wizard1_Load(object sender, EventArgs e)
    {
        // this.checkboxValidator();
        //checkworkplacechoice();
    }
    //protected void Button1_Click(object sender, EventArgs e)
    //{
    //    validtorPartsNum();
    //    Response.Write("<script>alert('获取服务成功，请点下一步！ ');</script>");
    //}
    /// <summary>
    /// 验证零件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btncheck_Click(object sender, EventArgs e)
    {
        if (GridView1.Rows.Count < 1)
        {
            Response.Write("<script>alert('请填写记录！零件数量类型错误！请先点显示！');history.back();</script>");
        }
        int checkcount = 0;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox check = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (check.Checked)
            {
                checkcount++;
            }
        }
        if (checkcount <= 0)
        {
            Response.Write("<script>alert('请选择！');history.back();</script>");
        }
        if (checkcount > 1)
        {
            Response.Write("<script>alert('请选中一个进行验证！');history.back();</script>");
        }
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox check = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (check.Checked)
            {
                try
                {
                    string partsId = GridView1.Rows[i].Cells[1].Text;
                    int txtnum = int.Parse(GridView1.Rows[i].Cells[2].Text);
                    SOAWebService.Service1 ser = new SOAWebService.Service1();
                    SOAWebService.Parts partsInfo = ser.getPartsById(partsId);
                    if (txtnum < partsInfo._partsNum)
                    {
                        Imgcheck.Visible = true;
                        lbcheck.Visible = true;
                        Imgcheck.ImageUrl = "Image/true.png";
                        lbcheck.Text = "库存满足所需。";
                    }
                    else
                    {
                        Imgcheck.Visible = true;
                        lbcheck.Visible = true;
                        Imgcheck.ImageUrl = "Image/false.png";
                        lbcheck.Text = "库存不足，到货日期为：" + partsInfo._partsarriveTime;
                        preturntime = (DateTime)partsInfo._partsarriveTime;
                        if (partsInfo._partsarriveTime > preturntime)
                        {
                            preturntime = (DateTime)partsInfo._partsarriveTime;


                        }

                    }
                }
                catch (Exception ee)
                {

                }
            }
        }

    }
    //protected void Button2_Click(object sender, EventArgs e)
    //{
    //    showmachine();
    //}
    /// <summary>
    /// 机器是否在使用状态
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnMcheck_Click(object sender, EventArgs e)
    {
        if (GridView2.Rows.Count < 1)
        {
            Response.Write("<script>alert('请填写记录！请先点显示！');history.back();</script>");
        }
        int checkcount = 0;
        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            CheckBox check = (CheckBox)GridView2.Rows[i].FindControl("Mcheck");
            if (check.Checked)
            {
                checkcount++;
            }
        }
        if (checkcount <= 0)
        {
            Response.Write("<script>alert('请选择！');history.back();</script>");
        }
        if (checkcount > 1)
        {
            Response.Write("<script>alert('请选中一个进行验证！');history.back();</script>");
        }
        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            CheckBox check = (CheckBox)GridView2.Rows[i].FindControl("Mcheck");
            if (check.Checked)
            {
                try
                {
                    string machineId = GridView2.Rows[i].Cells[1].Text;
                    SOAWebService.Service1 ser = new SOAWebService.Service1();
                    SOAWebService.Machine mac = ser.getMachineById(machineId);
                    if (mac._useState == true)
                    {
                        imgMcheck.Visible = true;
                        lbMcheck.Visible = true;
                        imgMcheck.ImageUrl = "Image/true.png";
                        lbMcheck.Text = "可以使用。";
                    }
                    else
                    {

                        imgMcheck.Visible = true;
                        lbMcheck.Visible = true;
                        imgMcheck.ImageUrl = "Image/false.png";
                        lbMcheck.Text = "正在使用。归还日期：" + mac._returnTime;
                        preturntime = (DateTime)mac._returnTime;
                        if (mac._returnTime > preturntime)
                        {
                            preturntime = (DateTime)mac._returnTime;


                        }
                    }
                }
                catch (Exception ee)
                {

                }
            }
        }
    }
    /// <summary>
    /// 获取工作场地信息
    /// </summary>
    public void getWorkplaceInfo()
    {
        SOAWebService.Service1 ser = new SOAWebService.Service1();
        SOAWebService.RepairPlace[] list = ser.getRepairPlace();
        GridView3.DataSource = list;
        GridView3.DataBind();


    }
    /// <summary>
    /// 提交场地需求
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button3_Click(object sender, EventArgs e)
    {
        int checkcount = 0;
        for (int i = 0; i < GridView3.Rows.Count; i++)
        {
            CheckBox check = (CheckBox)GridView3.Rows[i].FindControl("checkplace");
            if (check.Checked)
            {
                checkcount++;
            }
        }
        if (checkcount <= 0)
        {
            Response.Write("<script>alert('请选择！');history.back();</script>");
        }
        if (checkcount > 1)
        {
            Response.Write("<script>alert('请选中一个进行提交！');history.back();</script>");
        }
    }
    /// <summary>
    /// 向workplanparts表中插入数据
    /// </summary>
    public void insertWorkplanParts()
    {
        using (SOAWebService.Service1 ser = new SOAWebService.Service1())
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                ser.insertWorkplanParts(wId, GridView1.Rows[i].Cells[1].Text, int.Parse(GridView1.Rows[i].Cells[2].Text));
            }
        }
    }
    /// <summary>
    /// 向workplanmachine表中插入数据
    /// </summary>
    public void insertWorkplanMachine()
    {
        using (SOAWebService.Service1 ser = new SOAWebService.Service1())
        {
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
                ser.insertWorkplanMachine(wId, GridView2.Rows[i].Cells[1].Text);
            }
        }
    }
}
